package cn.kgc.vue.service.impl;

import cn.kgc.vue.aop.AopLog;
import cn.kgc.vue.aop.Opration;
import cn.kgc.vue.common.ResponseResult;
import cn.kgc.vue.common.ResponseStatus;
import cn.kgc.vue.entity.User;
import cn.kgc.vue.form.DeptLsitForm;
import cn.kgc.vue.mapper.UserMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.kgc.vue.entity.Dept;
import cn.kgc.vue.service.DeptService;
import cn.kgc.vue.mapper.DeptMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.HashMap;
import java.util.List;

/**
* @author 
* @description 针对表【dept】的数据库操作Service实现
* @createDate 2023-08-13 21:18:28
*/
@Service
public class DeptServiceImpl extends ServiceImpl<DeptMapper, Dept>
    implements DeptService{
    @Autowired
    private DeptMapper deptMapper;
    @Autowired
    private UserMapper userMapper;

    @Override
    @AopLog(opration = Opration.SELECT,model = "部门模块")
    public ResponseResult getlists(DeptLsitForm deptLsitForm) {
        ResponseResult responseResult = new ResponseResult();
        //模糊查询部门的信息
        Page<Dept> page = new Page<>(deptLsitForm.getPage(), deptLsitForm.getLimit());
        LambdaQueryWrapper<Dept> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(StringUtils.isNotEmpty(deptLsitForm.getDeptName()),Dept::getDeptName,deptLsitForm.getDeptName()).orderByDesc(Dept::getId);
        Page<Dept> deptPage = deptMapper.selectPage(page, wrapper);
        List<Dept> records = deptPage.getRecords();
        long total = deptPage.getTotal();
        HashMap<String, Object> map = new HashMap<>();
        map.put("total",total);
        map.put("dept",records);
        responseResult.success(ResponseStatus.SUCCESS,map);
        return responseResult;
    }

    @Override
    @AopLog(opration = Opration.ADD,model = "部门模块")
    public ResponseResult addorupdate(Dept dept) {
        ResponseResult responseResult = new ResponseResult();
        Integer id = dept.getId();
        if (ObjectUtils.isNotNull(id)){
            deptMapper.updateById(dept);
            responseResult.success();
            return responseResult;
        }
        LambdaQueryWrapper<Dept> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Dept::getDeptName,dept.getDeptName());
        Dept one = deptMapper.selectOne(wrapper);
        if(!ObjectUtils.isEmpty(one)){
            responseResult.fail(ResponseStatus.DEPT_EXIT);
            return responseResult;
        }
        deptMapper.insert(dept);
        responseResult.success(ResponseStatus.ADD_SUCCESS);
        return responseResult;
    }

    @Override
    public ResponseResult delete(String id) {
        ResponseResult responseResult = new ResponseResult();
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(User::getDeptId,id);
        List<User> users = userMapper.selectList(wrapper);
        if(!CollectionUtils.isEmpty(users)){
            responseResult.fail(ResponseStatus.DEPT_USER);
            return responseResult;
        }
        deptMapper.deleteById(id);
        responseResult.success(ResponseStatus.DELETE_SUCCESS);
        return responseResult;
    }
}




